package com.easyar.customizdblibrary;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import java.io.File;

/**
 * Created by Sigthp-P-Department on 2018/4/11.
 */

public class CustomizDBFactory {
    private static CustomizDBFactory mCustomizDBFactory=null;
    private final SQLiteDatabase mSqLiteDatabase;

    //String fileName,String dbName
    private CustomizDBFactory(Context context) {
        //找到数据库
        File dbRoot = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                +File.separator+FixedParameters.FILE_NAME+File.separator+"database");
        if(!dbRoot.exists()){
            dbRoot.mkdirs();
        }
        File dbFile=new File(dbRoot,FixedParameters.DB_NAME);

        //打开数据库
        mSqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);

    }


    public static CustomizDBFactory getFactory(Context context){
        if (mCustomizDBFactory == null){
            synchronized (CustomizDBFactory.class){
                if (mCustomizDBFactory == null){
                    mCustomizDBFactory = new CustomizDBFactory(context);
                }
            }
        }
        return mCustomizDBFactory;
    }

    public ICustomisDBDao getDao(Class clazz){
        ICustomisDBDao iCustomisDBDao=new CustomisDBDao();
        iCustomisDBDao.init(mSqLiteDatabase,clazz);
        return iCustomisDBDao;
    }

    public void closeDB(){
        mSqLiteDatabase.close();
    }
}
